#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
try:
    import ujson as json
except:
    import json
from cache.topic_gallery_cache import refresh_topic_gallery_cache
from controls import data_topic_gallery_mysql
from utils import log
from config import etc

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_secure_cookie("username")

class ManageTopicGalleryHandler(BaseHandler):
    def get(self):
        # if not self.current_user:
        #     self.redirect("/cms/signin")
        #     return
        return self.render('cms2/gallery.html')

    def post(self):
        try:
            provid = self.get_argument('provid', None)

            if provid is None:
                res = {'ret': 1, 'msg': 'param error'}
                self.write(json.dumps(res))
                self.finish()
                return

            gallery_list = data_topic_gallery_mysql\
                .get_topic_gallery_by_provid(provid)

            res = {'ret': 0, 'msg': '成功', 'res': gallery_list}
            self.write(json.dumps(res))
            self.finish()
            return
        except Exception as e:
            log.i(e)
            res = {'ret': 1, 'msg': '失败'}
            self.write(json.dumps(res))
            self.finish()
            return


class GalleryHandler(BaseHandler):
    def get(self):
        try:
            provid = self.get_argument('provid', None)

            if provid is None:
                res = {'ret': 1, 'msg': 'param error'}
                self.write(json.dumps(res))
                self.finish()
                return

            gallery_list = data_topic_gallery_mysql\
                .get_topic_gallery_by_provid(provid)

            res = {'ret': 0, 'msg': '成功', 'res': gallery_list}
            self.write(json.dumps(res))
            self.finish()
            return
        except Exception as e:
            log.i(e)
            res = {'ret': 1, 'msg': '失败'}
            self.write(json.dumps(res))
            self.finish()
            return


class TopicGalleryAddHandler(BaseHandler):
    def get(self):
        if not self.current_user:
            self.redirect("/cms/signin")
            return

    def post(self):
        try:
            provid = self.get_argument('provid', None)
            gallery_info_string = self.get_argument('gallery')

            if provid is None or not gallery_info_string:
                res = {'ret': 1, 'msg': 'param error'}
                self.write(json.dumps(res))
                self.finish()
                return

            gallery_info_list = json.loads(gallery_info_string)

            # 删除原来的
            data_topic_gallery_mysql.del_topic_gallery_item_by_provid(provid)

            # 增加新的
            for idx, gallery_info in enumerate(gallery_info_list):
                gallery_info['seq'] = idx
                gallery_info['provid'] = provid
                data_topic_gallery_mysql.add_topic_gallery_item(gallery_info)

            res = {'ret': 0, 'msg': '保存成功'}
            self.write(json.dumps(res))
            self.finish()
            return
        except Exception as e:
            log.i(e)
            res = {'ret': 1, 'msg': etc.err_500}
            self.write(json.dumps(res))
            self.finish()
            return

class TopicGalleryRefreshHandler(BaseHandler):
    def get(self):
        if not self.current_user:
            self.redirect("/cms/signin")
            return

    def post(self):
        try:
            provid = self.get_argument('provid', None)

            if provid is None:
                res = {'ret': 1, 'msg': 'param error'}
                self.write(json.dumps(res))
                self.finish()
                return

            refresh_topic_gallery_cache(provid)

            res = {'ret': 0, 'msg': '保存成功'}
            self.write(json.dumps(res))
            self.finish()
            return
        except Exception as e:
            log.i(e)
            res = {'ret': 1, 'msg': etc.err_500}
            self.write(json.dumps(res))
            self.finish()
            return